﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ContactManager.Common;
using System.Data.OleDb;

namespace ContactManager
{
    public partial class RegisterFrm : Form
    {
        public RegisterFrm()
        {
            InitializeComponent();
        }

        public RegisterFrm(LoginFrm frm)
        {
            this.loginfrm = frm;
            InitializeComponent();
        }

        private LoginFrm loginfrm = null;

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确认要跳过设置用户的步骤吗？", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            string id = GlobleUser.USER_ID.ToString();
            string username = this.txtUserName.Text.Trim();
            string userpass = this.txtUserPass.Text.Trim();
            string userpass2 = this.txtUserPassSecond.Text.Trim();

            //数据验证
            if (username == "")
            {
                MessageBox.Show("请输入用户名");
                return;
            }
            if (userpass == "")
            {
                MessageBox.Show("请输入密码");
                return;
            }
            if (userpass2 == "")
            {
                MessageBox.Show("请再一次输入密码");
                return;
            }
            if (userpass != userpass2)
            {
                MessageBox.Show("两次输入的密码不一致，请重新输入");
                this.txtUserPass.Text = "";
                this.txtUserPassSecond.Text = "";
                this.txtUserPass.Focus();
                return;
            }

            if (id.Length == 0)
            {
                string registerSql = "insert into users(user_name,user_pass) values(?,?)";
                OleDbParameter[] parameters = {
                                               new OleDbParameter("@user_name",username),
                                               new OleDbParameter("@user_pass",userpass)
                                               };
                int uId = AccessHelper.ExecuteInsert(registerSql, parameters);
                if (uId > 0)
                {
                    MessageBox.Show("设置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    GlobleUser.USER_ID = uId.ToString();
                    GlobleUser.USER_NAME = username;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("对不起，出错，请关闭重试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                string uSql = "update users set user_name=?,user_pass=? where user_id=?";
                OleDbParameter[] parameters = {
                                               new OleDbParameter("@user_name",username),
                                               new OleDbParameter("@user_pass",userpass),
                                               new OleDbParameter("@user_id",GlobleUser.USER_ID)
                                               };
                if (AccessHelper.ExecuteNonQuery(uSql, parameters) > 0)
                {
                    MessageBox.Show("修改成功，请重启程序生效", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("对不起，出错，请关闭重试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            
        }

        private void txtUserName_Leave(object sender, EventArgs e)
        {
            //string username = this.txtUserName.Text.Trim();
            //string sql = "select count(*) from users t where t.user_name = '" + username + "'";
            //if (AccessHelper.ExecuteScalar(sql) > 0)
            //{
            //    MessageBox.Show("对不起你输入的用户名已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    this.txtUserName.Text = "";
            //    this.txtUserName.Focus();
            //    return;
            //}
        }

        private void RegisterFrm_Load(object sender, EventArgs e)
        {
            //设置窗口位置
            this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, 0);

            this.Opacity = 0.8;//设置透明度

            

            int userNum = AccessHelper.ExecuteScalar("select count(*) from users");

            if (userNum > 0)
            {
                string sql = "select * from users where user_id=" + GlobleUser.USER_ID;
                DataSet ds = AccessHelper.ExecuteQuery(sql);
                DataRow dr = ds.Tables[0].Rows[0];
                
                this.txtUserName.Text = dr[1].ToString();
                
            }
        }


    }
}
